package com.wdy.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.wdy.dao.FacilityDao;
import com.wdy.po.Facility;
import com.wdy.service.FacilityManager;
import com.wdy.util.PageUtil;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;

@SuppressWarnings("unchecked")
public class FacilityManagerImpl implements FacilityManager {

	private FacilityDao facilityDao;

	public void setFacilityDao(FacilityDao facilityDao) {
		this.facilityDao = facilityDao;
	}

	public void saveFacility(Facility facility) {
		facilityDao.save(facility);
	}

	public void removeFacility(Facility facility) {
		facilityDao.delete(facility);
	}

	public void updateFacility(Facility facility) {
		facilityDao.attachDirty(facility);
	}

	public Facility getFacility(Integer id) {
		return facilityDao.findById(id);
	}

	public List<Facility> listAll() {
		return facilityDao.findAll();
	}

	public List<Facility> findByExample(Facility facility) {
		return facilityDao.findByExample(facility);
	}

	public List<Facility> findByName(String value) {
		return facilityDao.findByFname(value);
	}

	@Override
	public Result query(List<Condition> conList, Page page) {
		// TODO Auto-generated method stub
		int totalRecords = facilityDao.getQueryCount(conList);
		page = PageUtil.createPage(page, totalRecords);
		List tempList = (List) facilityDao.query(conList, page);
		List<Facility> facilitys = new LinkedList<Facility>();
		for (Object o : tempList) {
			if (o instanceof Object[]) {
				Object[] oList = (Object[]) o;
				for (Object oo : oList) {
					if (oo instanceof Facility) {
						facilitys.add((Facility) oo);
					}
				}
			} else {
				if (o instanceof Facility) {
					facilitys.add((Facility) o);
				}
			}
		}
		return new Result(page, facilitys);
	}

	@Override
	public void removeFacilitys(Object[] objects) {
		// TODO Auto-generated method stub
		facilityDao.deleteBatch(objects);
	}

}
